#|______________________________________________________________________________
 |
 | plots002.lsp
 |
 | Contains the following constructor functions for the new ViSta Plots System
 | All of these functions were previously undefined (no redefined functions)
 |
 |   frequency-distribution-plots   (histogram, frequency polygon, hollowgram)
 |   cumulative-distribution-plots  (quantile-plot and normal-probability-plot)    
 |   distribution-comparison-plot   (quantile-quantile plot)
 |   linkable-histogram-plot        (tierney's histogram)
 |______________________________________________________________________________
 |#



(defun frequency-distribution-plots
  (&optional data &key (ok-var-types '(numeric)) variable-labels 
             ;do not use :in?, :in??, or :in??? - reserved for system use
             (in nil in??) (in? nil in???) 
             (show t) (top-most t) 
             (location '(50 50)) (size '(320 320))
             (menu t) (title "Frequency Distribution Plot") 
             (legend1 (send $ :name)) (legend2 nil)
             (go-away t) (content-only nil)
             binwidth origin cutpoints histo prob hollow
             (new-x t) (new-y t) (bottom-tool-bar t)  sort)

"FREQUENCY DISTRIBUTION PLOTS makes polygon, histogram, and hollowgram plots from raw data, calculating frequencies from the raw data. 

ARGUMENTS:
&optional data &key binwidth origin cutpoints histo prob hollow new-x (new-y t) sort (ok-var-types '(numeric )) (title \"HistoFreq\") (in nil in?) (show t)  variable-labels point-labels (legend1 (send $ :name)) (legend2 \"Plot Title\")  (location '(50 50)) (size '(250 250)) (go-away t) (content-only nil) (help-bar nil) (pop-up t))

HISTOFREQ ARGS:
HISTO PROB BINWIDTH ORIGIN CUTPOINTS SORT BOTTOM-TOOL-BAR 
The initial plot is a frequency polygon, unless HISTO is T, in which case it is a histogram. An initial histogram is hollow when HOLLOW is T. The y-axis is initially probabilities if PROB is T, frequencies otherwise.
BINWIDTH ORIGIN CUTPOINTS must each be a list of values, one for each variable. Any given value may be nil. If not used the values will be calculated.  
When BOTTOM-TOOL-BAR is T buttons appear on a bar at the bottom. When NEW-X is T then a new-x button is put on tool-bar (default nil) to enable plotting variables other than the first;  When NEW-Y is T then a new-y button is put on the tool-bar to control y-axis representing frequencies or probabilities (default t).

GENERAL PLOT ARGUMENTS:
DATA may be nil, an object, a list, a vector, a list of lists, a list of vectors, or a matrix. If not specified, DATA is assumed to be and is converted to a list of vectors, one vector for each of the OK-VAR-TYPES variables in the current-data. Plot is of the first variable, initially, with the other variables available for plotting. The plot appears (or is prepared to appear if SHOW is NIL) in container IN, where IN is *DEFAULT-GRAPHICS-CONTAINER* if IN is not specified, the \"desktop\" if IN is T (i.e., popped out of *DEFAULT-GRAPHICS-CONTAINER*), the XLispStat window if IN is specified to be NIL, and container C if IN is specified to be the container window C. The plot will include variable-labels and legends, as specified. The plot will have a close box which will be de-activated when GO-AWAY is T. When CONTENT-ONLY is T the button bars, legends and overlays do not appear. When HELP-BAR is NIL the help-bar does not appear. When POP-UP is NIL the pop-up menus are not available."
  (let* ((actcon *active-container*)
         (in? (if in??? in? in??))
         (graph-frame (graph-frame :show nil))
         (pop-out (send graph-frame :seen-in in in?))
         (container (if (equal pop-out t) graph-frame pop-out))
         (graph-data (before-new-plot data ok-var-types ))
         (graph))
    (unless variable-labels (setf variable-labels (second graph-data)))
    (setf graph-data (first graph-data))
    (unless legend2 (setf legend2 title))
    (setf graph
          (send histofreq-proto :new graph-data 
                :variable-labels variable-labels 
                :show nil                        :top-most top-most
                :container container             :pop-out pop-out
                :location location               :size size 
                :menu menu                       :title title 
                :legend1 legend1                 :legend2 legend2
                :go-away go-away                 :content-only content-only
                ;MODULE-SPECIFIC ARGUMENTS
                :poly (not histo) :prob prob 
                :binwidth binwidth :origin origin :cutpoints cutpoints 
                :new-x new-x :new-y new-y :bottom-tool-bar bottom-tool-bar))
    (when graph
          (when (and (not hollow) histo) 
                (send graph :switch-plot-type)
                (send graph :switch-plot-type))
          (send graph :title title)
          (send graph :after-new-plot pop-out top-most show size container))
    graph))





(defun cumulative-distribution-plots
  (&optional data &rest args &key (ok-var-types '(numeric)) 
             (title "Cumulative Distribution Plot") 
             ;do not use :in?, :in??, or :in??? - reserved for system use
             (in nil in??) (in? nil in???) 
             (show t) variable-labels point-labels variable-label
             (legend1 (send current-object :name))  (legend2 " ") 
             location (size '(320 320)) (go-away t)
             (top-most t) (line-plot t) (points nil)
             (npplot nil) (content-only nil) (help-bar t) (pop-up t)
             nice-range (reg-line t))
"Args: (&optional data &rest args &key (ok-var-types '(numeric)) 
             (title \"Cumulative Distribution Plot\") 
             (in nil in?) (show t) variable-labels point-labels variable-label
             (legend1 (send current-object :name))  (legend2 \" \") 
             location (size '(250 250)) (go-away t) (line-plot t)
             (content-only nil) (help-bar nil) (pop-up t)
             nice-range (points nil) (reg-line t))

CUMULATIVE DISTRIBUTION PLOTS makes quantile plots and normal probability plots from numeric variables.;

DATA is nil, a data object, a sequence or a list of sequences. Makes quantile plot of the sequence or first sequence. By default a line plot is produced without a menu. Points and a menu are added if POINTS is T. When REG-LINE is t a regression line is to be added to the plot.  Range and tick-marks for y are determined by get-nice-range unless NICE-RANGE (list of min, max, num tick marks) is specified. Remaining arguments are the same as for scatterplots. Function written by Forrest Young to replace original by Luke Tierney."
(when (not variable-labels) (setf variable-labels variable-label))
  (let* ((actcon *active-container*)
         (in? (if in??? in? in??))
         (graph-frame (graph-frame :show nil))
         (pop-out (send graph-frame :seen-in in in?))
         (container (if (equal pop-out t) graph-frame pop-out))
         (graph-data (before-new-plot data ok-var-types))
         (graph))
    (unless variable-labels (setf variable-labels (second graph-data)))
    (unless point-labels (setf point-labels (third graph-data)))
    (setf graph-data (first graph-data))
    (unless legend2 (setf legend2 title))
    (setf graph
          (send quantile-plot-proto :new graph-data 
                :npplot T
                :title title 
                :point-labels point-labels
                :variable-labels variable-labels
                :reg-line reg-line 
                :nice-range nice-range
                :points points
                :location location 
                :size size
                :go-away go-away 
                :show nil
                :legend1 legend1 
                :legend2 legend2 )
              ;following not in isnew
              ; :line-plot line-plot
              ; :top-most top-most 
              ; :container container 
              ; :pop-out pop-out
              ; :content-only content-only
          )
    (when graph
          (send graph	:use-color t)
          (send graph :plot-buttons :new-x t :new-y t :free nil :mouse-mode nil)
          (when points
                (send graph :point-color (iseq (send graph :num-points)) 'blue)
                (send graph :mouse-mode 'brushing))
          (send graph :x-axis t t (third (send graph :x-axis)))
          (send graph :y-axis t t (third (send graph :y-axis)))
          (send graph :after-new-plot pop-out top-most show size container))
    graph))



(defun distribution-comparison-plot 
  (&optional data  &key (ok-var-types '(numeric)) 
             ;do not use :in?, :in??, or :in??? - reserved for system use
             (in nil in??) (in? nil in???) 
             (show t) (top-most t) 
             (location '(50 50)) (size '(320 320))
             (title "Distribution Comparison Plot") 
             (legend1 (send $ :name)) (legend2 nil)
             (go-away t) (reg-line t)
             (points nil) nice-x-range nice-y-range 
             (variable-labels (list "Ordered X Values" "Ordered Y Values"))  
             point-labels)
"Args:  nil, $, (list xdata ydata)  -or- xdata ydata
   &key points (title \"Quantile-Quantile Plot\") reg-line point-labels  variable-labels location size (go-away t) (show t) &rest args) 
DATA is nil, a data object, a list of two sequences (LIST XDATA YDATA), or two sequences  XDATA YDATA. In the latter case, the two sequences do not have to be the same length. When DATA is NIL the current data object is used. When  a data object is used the first two numeric variables are used. Makes a quantile-quantile plot of the two sequences.  By default a line plot is produced. Points are added if POINTS is T. When REG-LINE is t a regression line is added to the plot.  Range and tick-marks for x and y are determined by get-nice-range unless NICE-RANGE-X and NICE-RANGE-Y (each is a list of min, max, num tick marks) are specified. Remaining arguments are the same as for scatterplots. Function written by Forrest Young to replace original by Luke Tierney."
  (let* ((actcon *active-container*)
         (in? (if in??? in? in??))
         (graph-frame (graph-frame :show nil))
         (pop-out (send graph-frame :seen-in in in?))
         (container (if (equal pop-out t) graph-frame pop-out))
         (graph-data (before-new-plot data ok-var-types))
         (xdata)
         (ydata)
         (graph))
    (unless variable-labels (setf variable-labels (second graph-data)))
    (unless point-labels (setf point-labels (third graph-data)))
    (setf graph-data (first graph-data))
    (unless legend2 (setf legend2 "Quantile-Quantile Plot"))
    (setf xdata (first graph-data))
    (setf ydata (second graph-data))
    (setf graph 
          (send quantile-quantile-plot-proto :new xdata ydata :reg-line reg-line 
                 :nice-x-range nice-x-range :nice-y-range nice-y-range
                 :title title :point-labels point-labels
                 :legend1 legend1 :legend2 legend2
                 :show-points-and-menu points
                 :variable-labels variable-labels :location location 
                 :size size :go-away go-away :show nil :in in :in? in? ))
    (when graph
          (send graph :use-color t)
          (send graph :plot-buttons :new-x t :new-y t :free nil :mouse-mode points)
          (when points
                (send graph :point-color (iseq (send graph :num-points)) 'blue)
                (send graph :mouse-mode 'brushing))
          (send graph :x-axis t t (third (send graph :x-axis)))
          (send graph :y-axis t t (third (send graph :y-axis)))
          (send graph :after-new-plot pop-out top-most show size actcon))
    graph))


(defun linkable-histogram-plot
  (&optional data &key (title "Linkable Histogram Plot") 
             (ok-var-types '(numeric)) variable-labels 
             ;do not use :in?, :in??, or :in??? - reserved for system use
             (in nil in??) (in? nil in???) 
             (show t) (top-most t) 
             (location '(50 50)) (size '(320 320))
             (menu t) 
             (legend1 (send $ :name)) (legend2 nil)
             (go-away t) (content-only nil))

"LINKABLE-HISTOGRAM makes linkable histograms from frequencies calculated from raw data. 

ARGUMENTS:
&optional data &key (ok-var-types '(numeric )) (title \"Linkable Histogram\") (in nil in?) (show t)  variable-labels point-labels (legend1 (send $ :name)) (legend2 \"Plot Title\")  (location '(50 50)) (size '(320 320)) (go-away t) (content-only nil) (help-bar nil) (pop-up t))


GENERAL PLOT ARGUMENTS:
DATA may be nil, an object, a list, a vector, a list of lists, a list of vectors, or a matrix. If not specified, DATA is assumed to be and is converted to a list of vectors, one vector for each of the OK-VAR-TYPES variables in the current-data. Plot is of the first variable, initially, with the other variables available for plotting. The plot appears (or is prepared to appear if SHOW is NIL) in container IN, where IN is *DEFAULT-GRAPHICS-CONTAINER* if IN is not specified, the \"desktop\" if IN is T (i.e., popped out of *DEFAULT-GRAPHICS-CONTAINER*), the XLispStat window if IN is specified to be NIL, and container C if IN is specified to be the container window C. The plot will include variable-labels and legends, as specified. The plot will have a close box which will be de-activated when GO-AWAY is T. When CONTENT-ONLY is T the button bars, legends and overlays do not appear. When HELP-BAR is NIL the help-bar does not appear. When POP-UP is NIL the pop-up menus are not available."
      
  (let* ((actcon *active-container*)
         (in? (if in??? in? in??))
         (graph-frame (graph-frame :show nil))
         (pop-out (send graph-frame :seen-in in in?))
         (container (if (equal pop-out t) graph-frame pop-out))
         (graph-data (before-new-plot data ok-var-types ))
         (graph))
    (unless variable-labels (setf variable-labels (second graph-data)))
    (setf graph-data (first graph-data))
    (unless legend2 (setf legend2 title))
    (setf graph
          (histogram graph-data 
                     :variable-labels variable-labels
                     :title title
                     :location location
                     :size size 
                     :go-away go-away 
                     :show nil
                     :legend1 legend1 
                     :legend2 legend2
                     :top-most top-most
                     :container container
                   ;  :menu menu
                     :content-only content-only
                     ))
    (when graph
          (send graph :use-color t)
          (send graph :plot-buttons :new-x t :new-y nil :free nil)
          (send graph :point-color (iseq (send graph :num-points)) 'blue)
          (send graph :mouse-mode 'brushing)
          (send graph :x-axis t t (third (send graph :x-axis)))
          (send graph :y-axis t t (third (send graph :y-axis)))
          (send graph :title title)
          (send graph :after-new-plot pop-out top-most show size container))
    graph))
